#include "com_port.h"

// 禁用JTAG引脚,保留SWD引脚
void Sys_DisableJTAGEnableSWD(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 	//开启AFIO时钟
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //禁止JTAG功能
}

// PM 初始化
void Sys_PowerInit(void)
{
   	GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;	
    
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1|GPIO_Pin_12|GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

    Power_PM(0); Power_33V(0); Power_BL(0);
    
    // 唤醒/正在充电指示/充电完成
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13|GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_Init(GPIOC,&GPIO_InitStructure);    
    
    /* Configure one bit for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  
    /* 配置P[A|B|C|D|E]0为中断源 */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPD;
	GPIO_Init(GPIOC,&GPIO_InitStructure);   
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource15); 
    EXTI_InitStructure.EXTI_Line    = EXTI15_10_IRQn;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿中断
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure); 
}
// mcu 重新启动
void Sys_Restart(void){
    __set_FAULTMASK(1);
	NVIC_SystemReset();
}
void EXTI15_10_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line15) != RESET) //确保是否产生了EXTI Line中断
    {
        EXTI_ClearITPendingBit(EXTI_Line15);     //清除中断标志位
        
        if(SysParameter.RunS == 0)
            Sys_Restart();
    }  
}

void Sys_ReadIndicate(void){
   	if(SysParameter.RunS == Run) {        
        
        if(Read_PK1() == 1)
        {
            Delay_ms(20);
            if(Read_PK1() == 1)
            {
                while(Read_PK1());
                // 关机
                SysParameter.RunS = 0;
                Delay_ms(1000);
                Power_PM(0);
                Power_BL(0);
//                Power_33V(0);
            }
        }
        // 读取正在充电指示
        if(Read_PCHP() == 0)
            SysParameter.Charging = 1;
        else
            SysParameter.Charging = 0;
    }
    
    if(SysParameter.RunS == Standby)
    {
        if(Read_PK1() == 1)
        {
            Delay_ms(20);
            if(Read_PK1() == 1)
            {
                while(Read_PK1());
                // 关机
                SysParameter.RunS = Run;
            }
        }
        // 读取正在充电指示
        if(Read_PCHP() == 0)
            SysParameter.Charging = 1;
        else
        {
            SysParameter.Charging = 0;
            
            // 关机
            SysParameter.RunS = 0;
            Delay_ms(1000);
            Power_PM(0);
            Power_BL(0);
//            Power_33V(0);
        }            
    }
}




